/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:48
 */
package org.mechanic.fund.web.product;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.product.ProductStocksIndex;
import org.mechanic.fund.service.product.IProductStocksIndexService;
import org.mechanic.fund.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 基准信息 API
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api(value = "基准信息接口")
@RestController
@RequestMapping(value = "product/stocks_index")
public class ProductStocksIndexController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(ProductStocksIndexController.class);

    private IProductStocksIndexService productStocksIndexService;

    @Autowired
    public ProductStocksIndexController(IProductStocksIndexService productStocksIndexService) {
        this.productStocksIndexService = productStocksIndexService;
    }

    @ApiOperation(value = "返回每类基准信息最新的一条记录的集合")
    @GetMapping(value = "newests", produces = MediaTypes.JSON_UTF_8)
    public Object newests(
      @ApiParam(value = "页码(从0开始)", required = true) @RequestParam(required = true, defaultValue = "0") Integer page) {
        Page<ProductStocksIndex> stocksIndexPage = this.productStocksIndexService.getNewestListByType(
          super.pageRequest(page)
        );
        return result(stocksIndexPage);
    }

    @ApiOperation(value = "返回匹配指定基准信息类型的基准信息集合")
    @GetMapping(value = "listByTypeId/{typeId}", produces = MediaTypes.JSON_UTF_8)
    public Object listByTypeId(
      @ApiParam(value = "基准信息类型编号") @PathVariable(required = true) String typeId,
      @ApiParam(value = "页码(从0开始)", required = false) @RequestParam(required = false, defaultValue = "0") Integer page) {
        Page<ProductStocksIndex> stocksIndexPage = this.productStocksIndexService.getListByTypeId(
          typeId,
          super.pageRequest(page)
        );
        return result(stocksIndexPage);
    }

    @ApiOperation(value = "批量保存基准信息")
    @PutMapping(value = "save", produces = MediaTypes.JSON_UTF_8, consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "基准信息集合", required = true) @RequestBody(required = true)
        List<ProductStocksIndex> productStocksIndices) {
        this.productStocksIndexService.save(productStocksIndices, super.currUser().getId());
        return result();
    }

    @ApiOperation(value = "按 ID 删除基准信息")
    @DeleteMapping(value = "delete/{id}", produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "基准信息 ID", required = true) @PathVariable(required = true) String id) {
        this.productStocksIndexService.delete(id);
        return result();
    }
}
